package com.bicon.botu.finance.wallet.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.EncryptionUtil;
import com.bicon.botu.core.base.common.utils.StringUtils;
import com.bicon.botu.finance.wallet.common.FinanceConstants;
import com.bicon.botu.finance.wallet.common.WalletConstants;
import com.bicon.botu.finance.wallet.entity.Wallet;
import com.bicon.botu.finance.wallet.entity.WalletDetail;
import com.bicon.botu.finance.wallet.handler.WalletHandler;
import com.bicon.botu.finance.wallet.mapper.WalletDetailMapper;
import com.bicon.botu.finance.wallet.vo.WalletDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: sx
 * @Date: Created in 11:21 2017/6/21/021
 */
@Service
public class WalletDetailService extends ServiceImpl<WalletDetailMapper, WalletDetail> {

    @Autowired
    private WalletDetailMapper walletDetailMapper;

    @Autowired
    WalletHandler walletHandler;

    /**
     * 根据条件查询账单详情
     *
     * @param page
     * @param param
     * @return
     */
    public BaseResult selecetDetailByParam(Page<WalletDetail> page, Map<String, Object> param) {
        String userId = (String) param.get("userId");
        String walletId = (String) param.get("walletId");
        String subjects = (String) param.get("subjects");
        Date start = (Date) param.get("start");
        Date end = (Date) param.get("end");
        EntityWrapper<WalletDetail> entityWrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(userId)) {
            entityWrapper.eq("user_id", userId);
        }
        if (StringUtils.isNotBlank(walletId)) {
            entityWrapper.eq("wallet_id", walletId);
        }
        if (StringUtils.isNotBlank(subjects)) {
            entityWrapper.eq("subjects", subjects);
        }
        if (start != null && end != null) {
            entityWrapper.between("create_date", start, end);
        }
        entityWrapper.orderBy("create_date", false);
        Page<WalletDetail> walletDetailList = new WalletDetail().selectPage(page, entityWrapper);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, walletDetailList);
    }
    /**
     * 根据分类查询明细
     * @param pageNo
     * @param pageSize
     * @param subjects
     * @return
     */
    public BaseResult selectWalletDetailPage(int pageNo, int pageSize, String subjects) {
        Page<WalletDetailVo> page = new Page<WalletDetailVo>(pageNo, pageSize);
        List<WalletDetailVo> walletDetailVos = walletDetailMapper.selectWalletDetailPage(page, subjects);
        if (CollectionUtils.isEmpty(walletDetailVos)){
            return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, null);
        }
        for (WalletDetailVo walletDetailVo : walletDetailVos) {
            String name = walletHandler.getUserName(walletDetailVo.getUserId());
            if (null != name){
                walletDetailVo.setName(name);
            }
        }
        page.setRecords(walletDetailVos);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, page);
    }

    /**
     * 转账退款
     * @param wd 转账退款订单
     * @return
     */
    public boolean transferAccountsRefundTask(EntityWrapper entityWrapper, WalletDetail wd){
        //查询钱包
        Wallet wallet = new Wallet();
        wallet = wallet.selectOne(new EntityWrapper().eq("user_id", wd.getUserId()));
        BigDecimal money = wallet.getMoney().add(wd.getMoney());
        //修改订单状态为失败已退款
        wd.setState(WalletConstants.WALLET_STATE_FAIL);
        wd.preUpdate();
        boolean result = wd.update(entityWrapper);
        if(result){
            if(FinanceConstants.WALLET_SUBJECTS_PAY.equals(wd.getSubjects())){
                //是转账支出订单
                //给转账人db账户加钱
                wallet.setMoney(money);
                wallet.setMoneyMD5(EncryptionUtil.getMD5Str(money.toString()));
                wallet.preUpdate();
                result = wallet.updateById();
                if(result){
                    //刷新转账人redis账户金额
                    walletHandler.cacheBalance(wd.getUserId(), money);
                }
            }
        }
        return result;
    }
}
